package com.xcc.dataStructures.demo01_sparsearray;

/**
 * 稀疏数组
 *
 * @author xiaocheng
 * @date 2020/11/13 9:27
 */
public class SparseArray {

    public static void main(String[] args) {
        //定义11行11列的稀疏数组
        int[][] cherryArray = new int[11][11];
        //定义数值1为黑旗 2为蓝旗  在第2行第3列是黑子  第3行第4列是蓝子
        cherryArray[1][2] = 1;
        cherryArray[2][3] = 2;
        cherryArray[10][10] = 2;

        System.out.println("=================原始棋盘为================");
        for (int[] ints : cherryArray) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }

        System.out.println();
        //压缩为稀疏数组
        int[][] sparseArray = toSparseArray(cherryArray);

        System.out.println();
        //解析为原始数组
        parseCherryArray(sparseArray);
    }

    /**
     * 解压缩为原始数组
     * @param sparseArray    稀疏数组
     */
    private static void parseCherryArray(int[][] sparseArray) {
        System.out.println("=============解析后，稀疏数组为============");
        int[][] parseArray = new int[sparseArray[0][0]][sparseArray[0][1]];
        //记录解析的指针
        int count = 1;
        int sum = sparseArray[0][2];
        for (int i = 0; i < sparseArray.length; i++) {
            for (int j = 0; j < sparseArray[i].length; j++) {
                //判断值的个数应该小于等于总个数sum,以防止索引越界
                if (count <= sum) {
                    parseArray[sparseArray[count][0]][sparseArray[count][1]] = sparseArray[count][2];
                    count++;
                }
            }
        }
        //遍历解析后的数组
        for (int[] ints : parseArray) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }
    }

    /**
     * 压缩为稀疏数组
     * @param cherryArray   原始数组
     */
    private static int[][] toSparseArray(int[][] cherryArray) {
        System.out.println("=================稀疏数组为================");
        //定义稀疏数组为固定3列 行数根据有值的sum+1
        int sum = 0;
        for (int[] ints : cherryArray) {
            for (int anInt : ints) {
                if (anInt != 0) {
                    sum++;
                }
            }
        }
        int[][] sparseArray = new int[sum + 1][3];
        //获取稀疏数组第一行的数据
        sparseArray[0][0] = cherryArray.length;
        sparseArray[0][1] = cherryArray[0].length;
        sparseArray[0][2] = sum;
        //定义稀疏数组的行数指针
        int count = 0;
        for (int i = 0; i < cherryArray.length; i++) {
            for (int j = 0; j < cherryArray[i].length; j++) {
                if (cherryArray[i][j] != 0) {
                    count++;
                    sparseArray[count][0] = i;
                    sparseArray[count][1] = j;
                    sparseArray[count][2] = cherryArray[i][j];
                }
            }
        }
        //打印稀疏数组
        for (int[] ints : sparseArray) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }
        return sparseArray;
    }

}
